package day240602.e1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class User {
    private List<Student> students = new ArrayList<>();

    Scanner sc = new Scanner(System.in);

    File file = new File("src/day240602/e1/config.properties");
    Properties properties = new Properties();

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public void addStudent() {
        System.out.println("=== === ===");
        System.out.print("请输入学号：");
        int id = sc.nextInt();
        System.out.print("请输入姓名：");
        String name = sc.next();
        System.out.print("请输入年龄：");
        int age = sc.nextInt();

        Student student = new Student(id, name, age);

        students.add(student);
        System.out.println("添加成功");
        System.out.println("=== === ===");
    }

    public void removeStudent() {
        System.out.println("=== === ===");
        System.out.println("请输入要删除学生的学号；");
        int id = sc.nextInt();
        // TODO: 删除不成功
        for (Student student : students) {
            if (student.getId() == id) {
                students.remove(student);
                System.out.println("删除成功");
                break;
            }
        }
        System.out.println("=== === ===");
    }

    public void redactStudent() {
        System.out.println("=== === ===");
        System.out.println("请输入要修改学生的学号；");
        int id = sc.nextInt();
        // TODO: 查找不成功
        for (Student student : students) {
            if (student.getId() == id) {
                System.out.print("请输入新学号：");
                int newId = sc.nextInt();
                System.out.print("请输入新姓名：");
                String newName = sc.next();
                System.out.print("请输入新年龄：");
                int newAge = sc.nextInt();
                student.setId(newId);
                student.setName(newName);
                student.setAge(newAge);
                break;
            }
        }
        System.out.println("=== === ===");
    }

    public void printStudents() {
        System.out.println("=== === ===");
        System.out.println("请输入要查找学生的学号；");
        int id = sc.nextInt();
        // TODO: 查找不成功
        for (Student student : students) {
            if (student.getId() == id) {
                System.out.println(student);
            }
        }
        System.out.println("=== === ===");
    }

    public void printAllStudents() {
        System.out.println("=== === ===");
        for (Student student : students) {
            System.out.println(student);
        }
        System.out.println("=== === ===");
    }

    public void load() throws IOException {
        students.clear();
        properties.load(new FileInputStream(file));
        for (Object key : properties.keySet()) {
            int id = Integer.parseInt((String) key);
            String studentString = properties.getProperty((String) key);
            String[] split = studentString.split(",");
            String name = split[1];
            int age = Integer.parseInt(split[2]);
            students.add(new Student(id,name,age));
        }
    }

    public void save() throws IOException {
        properties.clear();
        for (Student student : students) {
            String key = String.valueOf(student.getId());
            String value = student.getId() + "," + student.getName() + "," + student.getAge();
            properties.put(key,value);
        }
        properties.store(new FileOutputStream(file), properties.toString());
    }

}
